var gulp = require('gulp');
var concat = require('gulp-concat');//合并
var uglify = require('gulp-uglify');//压缩
var rename = require('gulp-rename');//重命名
var less = require('gulp-less');//编译less
var cssmin = require('gulp-minify-css');//压缩css
var htmlmin = require('gulp-htmlmin');//压缩html
var imagemin = require('gulp-imagemin');//压缩图片
var connect = require('gulp-connect');//实现修改代码，浏览器自动刷新，实时预览
var clean = require('gulp-clean');//清空目录
//合并，压缩js
gulp.task("js",function(){
    // 把src目录下的所有js合并为main.js，压缩，重命名，输出到dist/js目录下
    gulp.src('src/js/**/*.js')
        .pipe(uglify({
            mangle: false,//类型：Boolean 默认：true 是否修改变量名
            compress: true,//类型：Boolean 默认：true 是否完全压缩
        }))
        .pipe(rename({suffix:'.min'}))
        .pipe(gulp.dest('dist/js'));
})

//编译，压缩less
gulp.task("css",function(){
    gulp.src(['src/css/app.less','src/css/include/**/*.less'])
        .pipe(less())
        .pipe(concat('app.css')) //合并css
        .pipe(cssmin()) //兼容IE7及以下需设置compatibility属性 .pipe(cssmin({compatibility: 'ie7'}))
        .pipe(rename({suffix:'.min'}))
        .pipe(gulp.dest('dist/css'));

    // 把src目录下的所有js合并为main.js，输出到dist/js目录下
    gulp.src(['src/css/page/**/*.less'])
        .pipe(less())
        //.pipe(concat('index.css')) //合并css
        .pipe(cssmin()) //兼容IE7及以下需设置compatibility属性 .pipe(cssmin({compatibility: 'ie7'}))
        .pipe(rename({suffix:'.min'}))
        .pipe(gulp.dest('dist/css/page'));
})


//图片压缩，拷贝到生成环境
gulp.task('image', function() {
    gulp.src('src/images/**/*.{png,jpg,gif}')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/images'));
});

//拷贝字体图标
gulp.task('font_icon', function(){
    gulp.src('src/css/font_icon/**/*')
        .pipe(gulp.dest('dist/css/font_icon'));
})

//拷贝lib
gulp.task('lib', function(){
    gulp.src('src/lib/**/*')
        .pipe(gulp.dest('dist/lib'));
})

//清空
gulp.task('clean', function(){
    gulp.src('dist')
                .pipe(clean());
})

//编译
gulp.task('build',['js','css','image','font_icon','lib']);


//实现源文件发生改变，目标文件内的文件也同步发生变化
//定义监听任务，watch为表示定义的监听任务
gulp.task('watch',function(){
    //表示index.html文件发生改变时，调用copyHtml这个任务
    gulp.watch('src/views/**/*.html',['html']);
    gulp.watch('src/js/**/*.js',['js']);
    gulp.watch(['src/css/app.less','src/css/include/**/*.less','src/css/page/**/*.less'],['css']);
    gulp.watch('src/images/**/*.{png,jpg}',['image']);
});

//gulp 默认命令
gulp.task('default',['js','css','image','font_icon','lib','watch'], function() {
    console.log('多个任务执行完毕');
});